﻿Imports System.Data.OleDb
Imports System.DBNull
Imports System.Data.SqlClient
Public Class DMNhanVien
    Public db As New Class1
    Dim gt As Boolean
    Public sql As String
    Public mycom As OleDbCommand
    Dim con As OleDbConnection
    Private Sub DMNhanVien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        db.connectDB()
        showDataGrid()
        khoa()
 
    End Sub
    Sub khoa()
        Me.manv.Enabled = False
        Me.tennv.Enabled = False
        Me.chucvu.Enabled = False
        Me.ngaysinh.Enabled = False
        Me.diachi.Enabled = False
        Me.mail.Enabled = False
        Me.sdt.Enabled = False
        Me.pass.Enabled = False

    End Sub
    Sub mo()
        Me.manv.Enabled = True
        Me.tennv.Enabled = True
        Me.chucvu.Enabled = True
        Me.ngaysinh.Enabled = True
        Me.diachi.Enabled = True
        Me.mail.Enabled = True
        Me.sdt.Enabled = True
        Me.pass.Enabled = True
    End Sub
    Sub xoa()
        Me.manv.Text = ""
        Me.tennv.Text = ""
        Me.chucvu.Text = ""
        ngaysinh.Value = System.DateTime.Now
        Me.diachi.Text = ""
        Me.mail.Text = ""
        Me.sdt.Text = ""
        Me.pass.Text = ""
    End Sub
    Function samekey()
        Dim lenh As String = " Select MaNV from NhanVien where MaNV='" & Me.manv.Text & "'"
        Dim mycom As New OleDbCommand(lenh, db.myconn)
        Dim mydr As OleDbDataReader = mycom.ExecuteReader()
        Return mydr.HasRows()
    End Function

    Private Sub them()
        Dim gioitinh As String
        If Me.nam.Checked = "True" Then
            gioitinh = "0"
        Else
            gioitinh = "1"
        End If
        showDataGrid()
        Dim sql As String = "insert into NhanVien values(N'" & Me.manv.Text.ToUpper & "',N'" & Me.tennv.Text & "',N'" & Me.chucvu.Text & "',N'" & Me.ngaysinh.Value.Date.ToString() & "',N'" & gioitinh & "',N'" & Me.diachi.Text & "',N'" & Me.mail.Text & "',N'" & Me.sdt.Text & "',N'" & Me.pass.Text & "');"
        If samekey() = True Then
            MsgBox("Mã Nhân Viên bạn nhập hiện đã có trong CSDL. Bạn vui lòng nhập lại!")
            Me.manv.Focus()
        Else
            db.doSQL(sql)
            MsgBox("Chúc mừng! Bạn đã thêm thành công!")
        End If
        showDataGrid()
    End Sub
    Sub sua()
        Dim gioitinh As String
        If Me.nam.Checked = "True" Then
            gioitinh = "0"
        Else
            gioitinh = "1"
        End If
        sql = "UPDATE NhanVien SET TenNV=N'" + Me.tennv.Text + "', ChucVu=N'" + Me.chucvu.Text + "', Ngaysinh=N'" + Me.ngaysinh.Value.Date.ToString() + "', Gioitinh=N'" + gioitinh + "', DiaChi=N'" + Me.diachi.Text + "', Email=N'" + Me.mail.Text + "', SDT=N'" + Me.sdt.Text + "', Pass=N'" + Me.pass.Text + "' where MaNV=N'" + Me.manv.Text.ToUpper + "'"
        db.doSQL(sql)
        Me.showDataGrid()
    End Sub
    Sub showDataGrid()
        sql = "Select  MaNV as 'Mã Nhân Viên' , TenNV as 'Tên Nhân Viên', ChucVu as 'Chức Vụ', Ngaysinh as 'Ngày Sinh', GioiTinh as 'Giới Tính', DiaChi as 'Địa Chỉ', Email as 'Email', SDT as 'Số Điện Thoại', Pass as 'Pass' From Nhanvien"
        db.iniDataset(sql)
        Me.viewnv.DataSource = db.mydataset.Tables(0)
    End Sub

    Private Sub themnv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        db.connectDB()
        xoa()
        btluu.Enabled = True
        bthuy.Enabled = True
        btthem.Enabled = False
        btsua.Enabled = False
        mo()
        gt = True
        Me.viewnv.Enabled = False
    End Sub

    Private Sub viewnhanvien_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles viewnv.CellContentClick
        Try
            Me.manv.Text = viewnv.CurrentRow.Cells(0).Value()
            Me.tennv.Text = viewnv.CurrentRow.Cells(1).Value()
            Me.chucvu.Text = viewnv.CurrentRow.Cells(2).Value()
            Me.ngaysinh.Value = viewnv.CurrentRow.Cells(3).Value()
            Me.diachi.Text = viewnv.CurrentRow.Cells(5).Value()
            Me.mail.Text = viewnv.CurrentRow.Cells(6).Value()
            Me.sdt.Text = viewnv.CurrentRow.Cells(7).Value()
            Me.pass.Text = viewnv.CurrentRow.Cells(8).Value()

        Catch ex As Exception
        End Try
    End Sub

    Private Sub xoanv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        db.connectDB()
        Dim gioitinh As String
        If Me.nam.Checked = "True" Then
            gioitinh = "0"
        Else
            gioitinh = "1"
        End If
           db.connectDB()
        sql = "delete From NhanVien where MaNV='" & Me.manv.Text & "'"
        If Me.manv.Text <> "" Then
            If MsgBox("Bạn có thật sự muốn xóa các thông tin về " & Me.manv.Text, MsgBoxStyle.OkCancel, "Xác nhận") = MsgBoxResult.Ok Then
                db.doSQL(sql)
            End If
        Else : MsgBox("Bạn chưa chọn thông tin cần xóa", MsgBoxStyle.Information, "Thông báo")
        End If
        Me.showDataGrid()
    End Sub

    Private Sub tknhanvien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttim.Click
        Dim timkiem As String = InputBox("Nhập từ khóa: ", "Tìm kiếm nhanh")

        If timkiem Is String.Empty Then
            showDataGrid()


        Else
            sql = "Select MaNV as 'Mã Nhân Viên' , TenNV as 'Tên Nhân Viên', ChucVu as 'Chức Vụ', Ngaysinh as 'Ngày Sinh', GioiTinh as 'Giới Tính', DiaChi as 'Địa Chỉ', Email as 'Email', SDT as 'Số Điện Thoại', Pass as 'Pass'" _
            & " FROM NhanVien " _
                    & "WHERE MaNV Like N'%" & timkiem & "%'    or   TenNV LIKE N'%" & timkiem & "%'   or   ChucVu LIKE N'%" & timkiem & "%'     or   Ngaysinh LIKE N'%" & timkiem & "%'    or   Gioitinh LIKE N'%" & timkiem & "%'    or   DiaChi LIKE N'%" & timkiem & "%'    or   Email LIKE N'%" & timkiem & "%'    or   SDT LIKE N'%" & timkiem & "%'    or   Pass LIKE N'%" & timkiem & "%'  "
            Try


                db.iniDataset(sql)
                viewnv.DataSource = db.mydataset.Tables(0)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        db.connectDB()
        Dim gioitinh As String
        If Me.nam.Checked = "True" Then
            gioitinh = "0"
        Else
            gioitinh = "1"
        End If
        If Me.manv.Text = "" Then
            MsgBox("chua chua truong de sua")
        Else
            Me.viewnv.Enabled = False
            btluu.Enabled = True
            bthuy.Enabled = True
            btthem.Enabled = False
            btsua.Enabled = False
            mo()
            Me.manv.Enabled = False
            gt = False
        End If
    End Sub

    Private Sub bthuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bthuy.Click
        btluu.Enabled = False
        bthuy.Enabled = False
        btthem.Enabled = True
        btsua.Enabled = True
        khoa()
        xoa()
        Me.viewnv.Enabled = True
    End Sub

    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        If gt = True Then
            them()
        Else
            sua()
        End If
    End Sub
End Class